package com.ebupt.migu.music.system.controller;

import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import com.ebupt.migu.music.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


@EnableAutoConfiguration
@RestController
@RequestMapping("/system/menu")
public class MenuController {
	@Autowired
    HttpServletRequest request;//这样使用，request每次进来都是Current Request
	
	@Autowired
	MenuService menuService;
    
    @RequestMapping("/list")
    public ResultObject<Object> list() {
    	ResultObject<Object> retObj = new ResultObject<>();
    	Map<String,Object> paramMap = ReqestParamUtil.getParams(request);
    	
    	retObj.setData(menuService.getMenuList(paramMap));
    	
    	return retObj;
    }
    
    @RequestMapping("/add")
    public ResultObject<Object> add() {
    	ResultObject<Object> retObj = new ResultObject<Object>();
		Map<String,Object> retMap = new HashMap<>(1);
    	Map<String,Object> paramMap = ReqestParamUtil.getParams(request);
    	String menuId = menuService.addMenu(paramMap);
		retMap.put("menuId", menuId);
    	retObj.setData(retMap);
		retObj.setMsg("新增菜单成功");
    	return retObj;
    }

	@RequestMapping("/modify")
	public ResultObject<Object> modify() {
		ResultObject<Object> retObj = new ResultObject<>();
		Map<String,Object> retMap = new HashMap<>(1);
		Map<String,Object> paramMap = ReqestParamUtil.getParams(request);

		String menuId = menuService.modifyMenu(paramMap);

		retMap.put("menuId", menuId);
		retObj.setData(retMap);
		retObj.setMsg("修改菜单成功");

		return retObj;
	}
    
    @RequestMapping("/delete")
    public ResultObject<Object> delete() {
    	ResultObject<Object> retObj = new ResultObject<>();
    	Map<String,Object> paramMap = ReqestParamUtil.getParams(request);
    	
    	menuService.delMenu(paramMap);
		retObj.setMsg("删除菜单成功");
    	
    	return retObj;
    }

}